<template> 
  <el-card class="form-container" shadow="never">
    <el-form :model="admin" ref="adminForm" label-width="150px" size="s米糕">
      <el-form-item label="活动名称：">
        <el-input v-model="admin.activityName"></el-input>
      </el-form-item>
      <el-form-item label="获奖名称：">
        <el-input v-model="admin.nameOfAward"></el-input>
      </el-form-item>

      <el-form-item label="证书持有者：">
        <el-input v-model="value.username" :disabled="true" class="input-w"></el-input>
        <el-button @click="selectUser">选择作者</el-button>
      </el-form-item>

      <el-form-item label="承办时间：">
        <el-date-picker placeholder="选择日期时间" type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="timestamp"
          v-model="admin.undertakingTime"></el-date-picker>
      </el-form-item>
      <el-form-item label="承办场地：">
        <el-input v-model="admin.undertakingSite"></el-input>
      </el-form-item>
      <el-form-item label="主办单位：">
        <el-input v-model="admin.organizer"></el-input>
      </el-form-item>
      <el-form-item label="活动视频：">
        <single-upload v-model="value.videoUrl"  @inputVideo="inputVideo" :ossUploadUrl="ossUploadUrl">
        </single-upload>
      </el-form-item>
      <el-form-item label="证书图片：">
        <image-upload v-model="value.pictures" @fileReceive="fileReceive">
        </image-upload>
      </el-form-item>

      <el-form-item label="是否启用：">
        <el-radio-group v-model="admin.status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="是否铸造：">
        <el-radio-group v-model="admin.mintStatus">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button size="s米糕">重置</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="s米糕">确 定</el-button>
      </el-form-item>
    </el-form>

    <user ref="userReference" :visible="visible" @updateEdit="updateEdit" @recevice="receiveData"></user>
  </el-card>
</template>

<script>
import { Message } from "element-ui";
import { receiveData, updateEdit, fileReceive, input, inputVideo, selectUser } from "../../../data/data"
import { add } from "../../../api/cert"
import {
  ADD_RULE_BY_CERT,
  ADD_RULE_BY_CERT_INFO
} from "../../../rules/addRuleByCert";
export default {
  name: "add",
  data() {
    return {
      admin: {
        userId: 0,
        activityName: undefined,
        nameOfAward: undefined,
        undertakingTime: undefined,
        undertakingSite: undefined,
        organizer: undefined,
        activityVideo: undefined,
        certificatePic: undefined,
        file: undefined
      },
      visible: false,
      disabled: false,
      ossUploadUrl: `${process.env.VUE_APP_SERVER}/aliyun/oss/uploadVideo`,
      value: {
        // 接收选择的用户
        uid: 0,
        username: undefined,
        pictures: undefined,
        file: undefined,
        videoUrl: undefined
      }
    }
  },

  components: {
    user: () => import("../../../components/SelectUser/index.vue"),
    singleUpload: () => import('@/components/Upload/singleUploadByVideo'),
    imageUpload: () => import('@/components/Upload/singleUploadByImage')
  },


  created() {
    // this.getBrandList();
  },
  watch: {
    selectProductCateValue: function (newValue) {
      if (newValue != null && newValue.length == 2) {
        this.listQuery.productCategoryId = newValue[1];
      } else {
        this.listQuery.productCategoryId = null;
      }

    },
    dialogVisible(val) {
      this.dialogVisible = val;
    }
  },


  methods: {
    selectUser,
    updateEdit,
    fileReceive,
    inputVideo,
    receiveData,
    input,

    checkForm(notice, data) {

      console.log("data", data);

      for (let key in data) {
        if (typeof data[key] === 'undefined') {
          Message({
            message: notice[key],
            duration: 3 * 1000
          });

          return false;
        }
      }

      return true;
    },

    handleDialogConfirm() {

      if (false === this.checkForm(ADD_RULE_BY_CERT, this.value)) {
        return;
      }

      this.admin.activityVideo = this.value.videoUrl;

      this.admin.certificatePic = this.value.pictures;

      this.admin.userId = this.value.uid

      this.admin.file = this.value.file.raw;

      console.log("this.admin", this.admin);

      this.admin.undertakingTime /= 1000;

      if (false === this.checkForm(ADD_RULE_BY_CERT_INFO, this.admin)) {
        return;
      }

      add(this.admin);
    },

    closeDialog(value) {
      console.log("value", value);

      this.$set(this, "dialogVisible", value);
    },
  }
}


</script>

<style type="less">
.input-w {
  width: 70%;
}

.margin-left {
  margin-left: -2%;
}
</style>